package com.aier.school.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.common.common.SystemDateFormatUtil;
import com.aier.school.common.TeacherLeaveType;
import com.aier.school.entity.TeacherLeave;

public class TeacherLeaveRowMapper implements RowMapper<TeacherLeave>{
    private int type;
    public static final int AND_TEACHERNAME = 1;
    public static final int AND_TEACHER = 2;
    
    public TeacherLeaveRowMapper() {
    }

    public TeacherLeaveRowMapper(int type) {
        this.type = type;
    }

    @Override
    public TeacherLeave mapRow(ResultSet rs, int rowNum) throws SQLException {
        TeacherLeave tl = null;
        switch (type) {
            case AND_TEACHERNAME:
                tl = parseTeacherLeave(new TeacherLeave(), rs, "tl");
                tl.setEuid_name(rs.getString("euid_name"));
                tl.setTeacherName(rs.getString("teacherName"));
                tl.settAccount(rs.getString("tAccount"));
                tl.setTypeName(rs.getString("typeName"));
                tl.setState_str(TeacherLeaveType.leaveStateMap.get(tl.getState()));
                tl.setType_handle_str(TeacherLeaveType.leaveTypeHandleMap.get(tl.getType_handle()));
                tl.setSdate_str(SystemDateFormatUtil.formatDate(tl.getSdate(), SystemDateFormatUtil.FORMAT_DATETIME_NOTSECOND));
                tl.setEdate_str(SystemDateFormatUtil.formatDate(tl.getEdate(), SystemDateFormatUtil.FORMAT_DATETIME_NOTSECOND));
                break;
            case AND_TEACHER:
                tl = parseTeacherLeave(new TeacherLeave(), rs, "tl");
                tl.setEuid_name(rs.getString("euid_name"));
                tl.setState_str(TeacherLeaveType.leaveStateMap.get(tl.getState()));
                tl.setType_handle_str(TeacherLeaveType.leaveTypeHandleMap.get(tl.getType_handle()));
                tl.setSdate_str(SystemDateFormatUtil.formatDate(tl.getSdate(), SystemDateFormatUtil.FORMAT_DATETIME_NOTSECOND));
                tl.setEdate_str(SystemDateFormatUtil.formatDate(tl.getEdate(), SystemDateFormatUtil.FORMAT_DATETIME_NOTSECOND));
                break;
            default:
                tl = parseTeacherLeave(new TeacherLeave(), rs);
        }
        return tl;
    }

    public static TeacherLeave parseTeacherLeave(TeacherLeave s, ResultSet rs) throws SQLException {
        return parseTeacherLeave(s, rs, null);
    }

    public static TeacherLeave parseTeacherLeave(TeacherLeave s, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            s.setLid(rs.getLong(su+"lid"));
        } catch (SQLException e) {
            s.setLid(null);
        }
        try {
            s.setTid(rs.getLong(su+"tid"));
        } catch (SQLException e) {
            s.setTid(null);
        }
        try {
            s.setState(rs.getShort(su+"state"));
        } catch (SQLException e) {
            s.setState(null);
        }
        try {
            s.setSdate(rs.getTimestamp(su+"sdate"));
        } catch (SQLException e) {
            s.setSdate(null);
        }
        try {
            s.setEdate(rs.getTimestamp(su+"edate"));
        } catch (SQLException e) {
            s.setEdate(null);
        }
        try {
            s.setSid(rs.getInt(su+"sid"));
        } catch (SQLException e) {
            s.setSid(null);
        }
        try {
            s.setLeaveTime(rs.getInt(su+"leaveTime"));
        } catch (SQLException e) {
            s.setLeaveTime(null);
        }
        try {
            s.setDesinfo(rs.getString(su+"desinfo"));
        } catch (SQLException e) {
            s.setDesinfo(null);
        }
        try {
            s.setType_handle(rs.getInt(su+"type_handle"));
        } catch (SQLException e) {
            s.setType_handle(null);
        }
        try {
            s.setEtid(rs.getLong(su+"etid"));
        } catch (SQLException e) {
            s.setEtid(null);
        }
        return s;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, TeacherLeave s) {
        par.put("lid", s.getLid());
        par.put("tid", s.getTid());
        par.put("state", s.getState());
        par.put("sdate",s.getSdate());        
        par.put("edate",s.getEdate());
        par.put("sid", s.getSid());
        par.put("leaveTime", s.getLeaveTime());
        par.put("desinfo", s.getDesinfo());
//        par.put("type_resone", s.getType_resone());
        par.put("type_handle", s.getType_handle());
        par.put("etid", s.getEtid());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, TeacherLeave s) {
        args.put("lid",s.getLid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final TeacherLeave s) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(s),
                        new String[] { "Lid" });
                parsePS(ps, s);
                return ps;
            }

        };
    }

    private static String parseSQL(TeacherLeave s) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into teacher_leave(");
        int count = 0;
        if (null != s.getLid()) {
            sql.append("lid,");
            count++;
        }
        if (null != s.getTid()) {
            sql.append("tid,");
            count++;
        }
        if (null != s.getState()) {
            sql.append("state,");
            count++;
        }
        if (null !=s.getSdate()) {
            sql.append("sdate,");
            count++;
        }        
        if (null !=s.getEdate()) {
            sql.append("edate,");
            count++;
        }
        if (null != s.getSid()) {
            sql.append("sid,");
            count++;
        }
        if (null != s.getLeaveTime()) {
            sql.append("leaveTime,");
            count++;
        }
        if (null != s.getDesinfo()) {
            sql.append("desinfo,");
            count++;
        }
//        if (null != s.getType_resone()) {
//            sql.append("type_resone,");
//            count++;
//        }
        if (null != s.getType_handle()) {
            sql.append("type_handle,");
            count++;
        }
        if (null != s.getEtid()) {
            sql.append("etid,");
            count++;
        }
        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, TeacherLeave s)
        throws SQLException {
        int count = 1;
        if (null != s.getLid()) {
            ps.setLong(count++,s.getLid());
        }
        if (null != s.getTid()) {
            ps.setLong(count++,s.getTid());
        }
        if (null != s.getState()) {
            ps.setShort(count++,s.getState());
        }
        if (null != s.getSdate()) {
            ps.setTimestamp(count++,s.getSdate());
    
        }
        if (null != s.getEdate()) {
            ps.setTimestamp(count++,s.getEdate());
        }        
        if (null != s.getSid()) {
            ps.setLong(count++,s.getSid());
        }
        if (null != s.getLeaveTime()) {
            ps.setInt(count++,s.getLeaveTime());
        }
        if (null != s.getDesinfo()) {
            ps.setString(count++,s.getDesinfo());
        }
//        if (null != s.getType_resone()) {
//            ps.setInt(count++,s.getType_resone());
//        }        
        if (null != s.getType_handle()) {
            ps.setInt(count++,s.getType_handle());
        }    
        if (null != s.getEtid()) {
            ps.setLong(count++,s.getEtid());
        } 
    }
}
